import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import sklearn.linear_model as lm
import sklearn.metrics as sm
data = pd.read_csv('../data_test/Salary_Data.csv')
# plt.scatter(data['YearsExperience'],data['Salary'])
# plt.show()

#整理数据，输入数据二维，输出数据一维
train_x = data.iloc[:,:-1]
train_y = data.iloc[:,-1]
model = lm.LinearRegression() #构建模型

model.fit(train_x,train_y) #训练模型

# pred_train_y = model.predict(train_x) #预测模型

# plt.plot(train_x,pred_train_y,c='orangered')
# plt.scatter(data['YearsExperience'],data['Salary'])
# plt.show()

#拿测试数据去评估模型
test_x = train_x.iloc[::4]
test_y = train_y[::4]
pred_test_y = model.predict(test_x)
#平均绝对误差（Mean Absolute Deviation）
print(sm.mean_absolute_error(test_y, pred_test_y))

#均方误差接口
print(sm.mean_squared_error(test_y, pred_test_y))

#MAD(中位数绝对偏差)：与数据中值绝对偏差的中值；
print(sm.median_absolute_error(test_y, pred_test_y))
#R2决定系数：趋向于1，模型越好；趋向于0，模型越差.
print(sm.r2_score(test_y, pred_test_y))





